package javanet.context;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Markus Holzer
 */
public class JavaNETContextHolder {

	private static final ThreadLocal<Map<JavaNETRegistryKey, Object>> THREAD_WITH_CONTEXT = 
		new ThreadLocal<Map<JavaNETRegistryKey, Object>>();

	private JavaNETContextHolder() {
	}

	public static void put(JavaNETRegistryKey key, Object object) {
		if (THREAD_WITH_CONTEXT.get() == null) {
			THREAD_WITH_CONTEXT.set(new HashMap<JavaNETRegistryKey, Object>());
		}
		THREAD_WITH_CONTEXT.get().put(key, object);
	}

	public static Object get(JavaNETRegistryKey key) {
		try {
			return THREAD_WITH_CONTEXT.get().get(key);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void cleanupThread() {
		THREAD_WITH_CONTEXT.remove();
	}

	public static JavaNETFacadeContext getFacadeContext() {
		return (JavaNETFacadeContext)get(JavaNETRegistryKey.FACADE_CONTEXT);
	}

	public static void setFacadeContext(JavaNETFacadeContext context) {
		put(JavaNETRegistryKey.FACADE_CONTEXT, context);
	}

}
